//
//  FineCell.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/6/22.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
class FineCell: UITableViewCell {
    var license_1,submitFine1,license_2,license_3,submitFine2:UIButton!
    var view1,view2:UIView!
    var remark1,remark2:UITextView!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCellSelectionStyle.none
        self.layer.borderWidth = 0
        let cfc = CarFineController()
        //************************************有单据************************************
        view1 = UIView.init(frame: CGRect(x: 0, y: cellH, width: kScreenW, height: 3*oTop + oLabelH + 2*oImgH + remarkH + cellH + 10))
        self.addSubview(view1)
        let label1 = UILabel.init(frame: CGRect(x: oLabelLeft, y: 0, width: 120, height: cellH))
        label1.font = oFontSize
        label1.text = "罚款单据信息"
        view1.addSubview(label1)
        
        license_1 = UIButton.init(frame: CGRect(x: oImgLeft, y: oTop + oLabelH, width: oImgW, height: 2*oImgH))
        license_1.setBackgroundImage(UIImage(named:"car_fine"), for: .normal)
        license_1.tag = 1
        license_1.addTarget(cfc, action: #selector(cfc.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        view1.addSubview(license_1)
        
        let remarkLabel1 = UILabel.init(frame: CGRect(x: oLabelLeft, y: 2*oTop + oLabelH + 2*oImgH, width: oLabelW, height: remarkH))
        remarkLabel1.font = oFontSize
        remarkLabel1.text = "备注信息"
        view1.addSubview(remarkLabel1)
        
        remark1 = UITextView(frame:CGRect(x:oLabelW, y:2*oTop + oLabelH + 2*oImgH, width:oImgW, height:remarkH))
        remark1.layer.cornerRadius = 5
        remark1.backgroundColor = tableBgColor
        remark1.textColor = fontColor
        remark1.font = oFontSize
        remark1.delegate = self
        view1.addSubview(remark1)
        
        submitFine1 = UIButton.init(frame:CGRect(x: oLabelLeft, y: 4*oTop + oLabelH + 2*oImgH + remarkH, width: kScreenW - 2*oLabelLeft, height: cellH))
        submitFine1.setTitle("提交", for: .normal)
        submitFine1.backgroundColor = appMainColor
        submitFine1.showsTouchWhenHighlighted = true
        submitFine1.layer.cornerRadius = 10;
        submitFine1.addTarget(cfc, action: #selector(cfc.haveFineFunc), for: .touchUpInside)
        view1.addSubview(submitFine1)
        //************************************没有单据************************************
        view2 = UIView.init(frame: CGRect(x: 0, y: cellH, width: kScreenW, height: 2*cellH + oLabelH + 2*oImgH + 4*oTop + remarkH + 10))
        self.addSubview(view2)
        view2.isHidden = true
        view2.addSubview(getSeparator(index: 0))
        let label5 = UILabel.init(frame: CGRect(x: oLabelLeft, y: 0, width: oLabelW, height: cellH))
        label5.font = oFontSize
        label5.text = "行驶证信息"
        view2.addSubview(label5)
        
        license_2 = UIButton.init(frame: CGRect(x: oImgLeft, y:oLabelH + oTop, width: oImgW, height: oImgH))
        license_2.setBackgroundImage(UIImage(named:"xingshizheng1"), for: .normal)
        license_2.tag = 2
        license_2.addTarget(cfc, action: #selector(cfc.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        view2.addSubview(license_2)
        
        license_3 = UIButton.init(frame: CGRect(x: oImgLeft, y: oLabelH + 2*oTop + oImgH, width: oImgW, height: oImgH))
        license_3.setBackgroundImage(UIImage(named:"xingshizheng2"), for: .normal)
        license_3.tag = 3
        license_3.addTarget(cfc, action: #selector(cfc.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        view2.addSubview(license_3)
        
        let remarkLabel2 = UILabel.init(frame: CGRect(x: oLabelLeft, y: oLabelH + 2*oImgH + 2*oTop, width: oLabelW, height: 2*cellH))
        remarkLabel2.font = oFontSize
        remarkLabel2.text = "备注信息"
        view2.addSubview(remarkLabel2)
        
        remark2 = UITextView(frame:CGRect(x:oLabelW, y:oLabelH + 2*oImgH + 3*oTop, width:oImgW, height:remarkH))
        remark2.layer.cornerRadius = 5
        remark2.backgroundColor = tableBgColor
        remark2.textColor = fontColor
        remark2.font = oFontSize
        remark2.delegate = self
        view2.addSubview(remark2)
        
        submitFine2 = UIButton.init(frame:CGRect(x: oLabelLeft, y:oLabelH + 2*oImgH + 5*oTop + remarkH, width: kScreenW - 2*oLabelLeft, height: cellH))
        submitFine2.setTitle("提交", for: .normal)
        submitFine2.backgroundColor = appMainColor
        submitFine2.showsTouchWhenHighlighted = true
        submitFine2.layer.cornerRadius = 10;
        submitFine2.addTarget(cfc, action: #selector(cfc.noFineFunc), for: .touchUpInside)
        view2.addSubview(submitFine2)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension FineCell:UITextViewDelegate{
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        currentTagY = textView.frame.origin.y + 2*cellH
        return true
    }
}
extension FineCell{
    func getCellY(index : Int) -> CGFloat {
        return cellH * CGFloat(index)
    }
    func getSeparator(index: Int) -> UILabel {
        let lab = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: index), width: kScreenW - 2*oLabelLeft, height: 1))
        lab.backgroundColor = separatorColor
        return lab
    }
}
